//
//  ESTCustomerInfoModel.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/10/15.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTCustomerInfoModel.h"

@implementation ESTCustomerInfoModel

/**
 获取我的业绩
 
 @param itemCode 业务类型
 @param activity_id 活动ID
 @param start_date 开始时间
 @param end_date 结束时间
 @param page_num 页码
 @param success 成功回调
 @param failure 失败回调
 */
+ (void)requestAchieveDataWithItemCode:(NSString *)itemCode activity_id:(NSString *)activity_id start_date:(NSString *)start_date end_date:(NSString *)end_date page_num:(NSInteger)page_num success:(void(^)(NSArray<ESTCustomerInfoModel *> *modelArray, NSInteger dataNumber))success failure:(void(^)(NSString *error))failure {
    NSDictionary *dict = @{
                           @"BUS_ITEM_CODE" : itemCode,
                           @"ACTIVITY_ID" : activity_id,
                           @"START_DATE" : start_date,
                           @"END_DATE" : end_date,
                           @"GET_COUNT" : @(0),
                           @"PAGE_NUM" : @(page_num),
                           @"PAGE_SIZE" : @(Request_PageSize_Normal)
                           };
    [NetTool postWithCode:Code_Activity_Achieve parameter:dict success:^(NSDictionary *result) {
        NSInteger dataNumber = [result[@"DATA_COUNT"] integerValue];
        NSArray *arr = [NSArray arrayWithArray:result[@"DATA_INFO"]];
        NSMutableArray *arrayM = [ESTCustomerInfoModel mj_objectArrayWithKeyValuesArray:arr];
        if (success) {
            success([arrayM mutableCopy], dataNumber);
        }
    } failure:^(NSString *error) {
        if (failure) {
            failure(error);
        }
    }];
}

/**
 搜索客户信息

 @param keyWords 搜索的关键字
 @param activity_id 活动ID（可以填nil）
 @param orderbyType 排序方式
 @param orderbyDesc 是否降序
 @param success 成功回调
 @param failure 失败回到
 */
+ (void)searchCustomerListDataWithKeyWords:(NSString *)keyWords activity_id:(NSString *)activity_id orderbyType:(NSString *)orderbyType orderbyDesc:(BOOL)orderbyDesc success:(void(^)(NSArray<ESTCustomerInfoModel *> *modelArray, NSInteger dataNumber))success failure:(void(^)(NSString *error))failure {
    if (!keyWords || !orderbyType) return;
    NSDictionary *dict = @{
                           @"KEYWORD" : keyWords,
                           @"ORDERBY_TYPE" : orderbyType,
                           @"ORDERBY_DESC" : @(orderbyDesc),
                           @"GET_COUNT" : @(1)
                           };
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    [dictM setDictionary:dict];
    if (activity_id) {
        [dictM setObject:activity_id forKey:@"ACTIVITY_ID"];
    }
    [self sendParameter:[dictM mutableCopy] success:success failure:failure];
}

/**
 简单搜索

 @param keyWords 关键字
 @param success 成功回调
 @param failure 失败回调
 */
+ (void)searchCustomerListDataWithKeyWords:(NSString *)keyWords success:(void(^)(NSArray<ESTCustomerInfoModel *> *modelArray, NSInteger dataNumber))success failure:(void(^)(NSString *error))failure {
    if (!keyWords) return;
    NSDictionary *dict = @{
                           @"KEYWORD" : keyWords,
                           @"GET_COUNT" : @(1)
                           };
    [self sendParameter:dict success:success failure:failure];
}

/**
 查询单个客户信息

 @param customerID 客户ID
 @param success 成功回调
 @param failure 失败回调
 */
+ (void)enquiriesCustomerInfoWithID:(NSString *)customerID success:(void(^)(NSArray<ESTCustomerInfoModel *> *modelArray, NSInteger dataNumber))success failure:(void(^)(NSString *error))failure {
    if (customerID.length > 0) {
        NSDictionary *dict = @{
                               @"CUST_ID" : customerID,
                               @"GET_COUNT" : @(1)
                               };
        [self sendParameter:dict success:success failure:failure];
    } else {
        failure(Parameter_Error);
    }
}

/**
 查询跟进的客户列表

 @param followUpmode 跟进的类型
 @param activityID 活动ID（可以填nil，表示查询所有活动下的客户）
 @param orderbyType 排序方式
 @param orderbyDesc 是否降序
 @param followUpLabel 客户标签
 @param page_num 第几页
 @param success 成功回调
 @param failure 失败回调
 */
+ (void)requestFollowUpListDataWithFollowUpMode:(ESTFollowUpMode)followUpmode activityID:(NSString *)activityID orderbyType:(NSString *)orderbyType orderbyDesc:(BOOL)orderbyDesc followUpLabel:(NSString *)followUpLabel page_num:(NSInteger)page_num success:(void(^)(NSArray<ESTCustomerInfoModel *> *modelArray, NSInteger dataNumber))success failure:(void(^)(NSString *error))failure {
    if (!orderbyType) {
        if (failure) {
            failure([@"缺少排序方式" S]);
        }
        return;
    }
    NSString *IS_FIRST_FOLLOWUP = followUpmode == FollowUpMode_First ? @"1" : @"0";
    NSString *IS_PLANCOME = followUpmode == FollowUpMode_Coming ? @"1" : @"";
    NSString *FOLLOWUP_LABLE = followUpLabel;
    NSString *ACTIVITY_ID = activityID;
    NSDictionary *dict = @{
                           @"IS_FIRST_FOLLOWUP" : IS_FIRST_FOLLOWUP,
                           @"IS_PLANCOME" : IS_PLANCOME,
                           @"ORDERBY_TYPE" : orderbyType,
                           @"ORDERBY_DESC" : orderbyDesc ? @"1" : @"0",
                           @"GET_COUNT" : @(1),
                           @"PAGE_NUM" : @(page_num),
                           @"PAGE_SIZE" : @(Request_PageSize_Normal)
                           };
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    [dictM setDictionary:dict];
    if (ACTIVITY_ID) {
        [dictM setObject:ACTIVITY_ID forKey:@"ACTIVITY_ID"];
    }
    if (FOLLOWUP_LABLE.length > 0) {
        [dictM setObject:FOLLOWUP_LABLE forKey:@"FOLLOWUP_LABLE"];
    }
    [self sendParameter:[dictM mutableCopy] success:success failure:failure];
}

/**
 发送查询客户信息的请求

 @param parameter 参数
 @param success 成功回调
 @param failure 失败回调
 */
+ (void)sendParameter:(NSDictionary *)parameter success:(void(^)(NSArray<ESTCustomerInfoModel *> *modelArray, NSInteger dataNumber))success failure:(void(^)(NSString *error))failure {
    [NetTool postWithCode:Code_Activity_CUSList parameter:parameter success:^(NSDictionary *result) {
        NSInteger dataNumber = [result[@"DATA_COUNT"] integerValue];
        NSArray *arr = [NSArray arrayWithArray:result[@"DATA_INFO"]];
        NSMutableArray *arrayM = [ESTCustomerInfoModel mj_objectArrayWithKeyValuesArray:arr];
        if (success) {
            success([arrayM mutableCopy], dataNumber);
        }
    } failure:^(NSString *error) {
        if (failure) {
            failure(error);
        }
    }];
}

/**
 保存跟进信息

 @param model 用户信息模型
 @param duration 通话时长
 @param guid guid
 @param complement 完成回调
 */
+ (void)preserveCustomerInfoWithModel:(ESTCustomerInfoModel *)model duration:(NSString *)duration guid:(NSString *)guid complement:(void(^)(NSString *message))complement {
    NSDictionary *dict = @{
                           @"CUST_ID" : model.VE_ACTIVITY_CUST_ID ? model.VE_ACTIVITY_CUST_ID : @"",
                           @"ACTIVITY_ID" : model.VE_ACTIVITY_ID ? model.VE_ACTIVITY_ID : @"",
                           @"CUST_NAME" : model.CUST_NAME ? model.CUST_NAME : @"",
                           @"CUST_PHONE" : model.CUST_PHONE ? model.CUST_PHONE : @"",
                           @"SOURCE_BIG_CODE" : model.SOURCE_BIG_CODE ? model.SOURCE_BIG_CODE : @"",
                           @"SOURCE_SMALL_CODE" : model.SOURCE_SMALL_CODE ? model.SOURCE_SMALL_CODE : @"",
                           @"CUST_SEX" : model.CUST_SEX ? model.CUST_SEX : @"",
                           @"IS_INTENT" : model.IS_INTENT,
                           @"INTENT_CARTYPE_ID" : model.INTENT_CARTYPE_ID ? model.INTENT_CARTYPE_ID : @"",
                           @"INTENT_CARTYPE_CN" : model.INTENT_CARTYPE_CN ?  model.INTENT_CARTYPE_CN : @"",
                           @"IS_ADDWECHAT" : model.IS_ADDWECHAT,
                           @"WECHAT" : model.WECHAT ? model.WECHAT : @"",
                           @"IS_PLANCOME" : model.IS_PLANCOME,
                           @"PLAN_COME_TIME" : model.PLAN_COME_TIME ? model.PLAN_COME_TIME : @"",
                           @"PLAN_FOLLOWUP_TIME" : model.PLAN_FOLLOWUP_TIME ? model.PLAN_FOLLOWUP_TIME : @"",
                           @"IS_COME" : model.IS_COME,
                           @"REAL_COME_TIME" : model.REAL_COME_TIME ? model.REAL_COME_TIME : @"",
                           @"IS_ORDER" : model.IS_ORDER,
                           @"IS_PAY" : model.IS_PAY,
                           @"PAY_MONEY" : model.PAY_MONEY ? model.PAY_MONEY : @"",
                           @"IS_FAIL" : model.IS_FAIL,
                           @"FAIL_DESC" : model.FAIL_DESC ? model.FAIL_DESC : @"",
                           @"IS_READYCOME" : model.IS_READYCOME,
                           @"IS_NOLINK" : model.IS_NOLINK,
                           @"FOLLOWUP_LABLE" : model.FOLLOWUP_LABLE ? model.FOLLOWUP_LABLE : @"",
                           @"FOLLOWUP_CONTENT" : model.FOLLOWUP_CONTENT ? model.FOLLOWUP_CONTENT : @"",
                           @"FOLLOWUP_DURATION" : duration ? duration : @"",
                           @"PLAN_COME_NUMBER" : model.PLAN_COME_NUMBER ? model.PLAN_COME_NUMBER : @"",
                           @"INTERCURRENT_ID" : guid ? guid : @"12345678901234567890123456789012",
                           };
    [NetTool postWithCode:Code_Activity_FollowUp parameter:dict success:^(NSDictionary *result) {
        if (complement) {
            complement(Request_Success);
        }
    } failure:^(NSString *error) {
        if (complement) {
            complement(error);
        }
    }];
}
- (NSString *)IS_INTENT {
    if (_IS_INTENT == nil) {
        _IS_INTENT = @"0";
    }
    return _IS_INTENT;
}
- (NSString *)IS_ADDWECHAT {
    if (_IS_ADDWECHAT == nil) {
        _IS_ADDWECHAT = @"0";
    }
    return _IS_ADDWECHAT;
}
- (NSString *)IS_PAY {
    if (_IS_PAY == nil) {
        _IS_PAY = @"0";
    }
    return _IS_PAY;
}
- (NSString *)IS_ORDER {
    if (_IS_ORDER == nil) {
        _IS_ORDER = @"0";
    }
    return _IS_ORDER;
}
- (NSString *)IS_PLANCOME {
    if (_IS_PLANCOME == nil) {
        _IS_PLANCOME = @"0";
    }
    return _IS_PLANCOME;
}
- (NSString *)IS_READYCOME {
    if (_IS_READYCOME == nil) {
        _IS_READYCOME = @"0";
    }
    return _IS_READYCOME;
}
- (NSString *)IS_COME {
    if (_IS_COME == nil) {
        _IS_COME = @"0";
    }
    return _IS_COME;
}
- (NSString *)IS_NOLINK {
    if (_IS_NOLINK == nil) {
        _IS_NOLINK = @"0";
    }
    return _IS_NOLINK;
}
- (NSString *)IS_FAIL {
    if (_IS_FAIL == nil) {
        _IS_FAIL = @"0";
    }
    return _IS_FAIL;
}
@end
